<?php

namespace App\Models\Recognize;

use App\Models\BaseModel;

class RecognizeBusinessLicenseResult extends BaseModel
{
    protected $table = 'recognize_business_license_results';

    protected $fillable = ['url'];

    public static function add(string $urlPath, array $recognizeResult): RecognizeBusinessLicenseResult
    {
        $m = static::firstOrNew([
            'url' => $urlPath,
        ]);
        if (8 == strlen($recognizeResult['ValidPeriod'])) {
            $m->validPeriod = mb_substr($recognizeResult['ValidPeriod'], 0, 4) . '-' . mb_substr($recognizeResult['ValidPeriod'], 4, 2) . '-' . mb_substr($recognizeResult['ValidPeriod'], 6, 2);
        }
        $m->address     = mb_substr($recognizeResult['Address'], 0, 255);
        $m->capital     = mb_substr($recognizeResult['Capital'], 0, 32);
        $m->legalPerson = mb_substr($recognizeResult['LegalPerson'], 0, 32);
        if (8 == strlen($recognizeResult['EstablishDate'])) {
            $m->establishDate = mb_substr($recognizeResult['EstablishDate'], 0, 4) . '-' . mb_substr($recognizeResult['EstablishDate'], 4, 2) . '-' . mb_substr($recognizeResult['EstablishDate'], 6, 2);
        }
        $m->name           = mb_substr($recognizeResult['Name'], 0, 64);
        $m->registerNumber = mb_substr($recognizeResult['RegisterNumber'], 0, 32);
        $m->type           = mb_substr($recognizeResult['Type'], 0, 32);
        $m->business       = mb_substr($recognizeResult['Business'], 0, 500);
        $m->save();
        return $m;
    }
}
